package com.example.controller;

import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.service.MobileService;
import com.example.utils.JwtUtils;
import com.example.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@RequestMapping("/mobile")
@RestController
public class MobileController {
    @Autowired
    private MobileService mobileService;
    @RequestMapping("/register")
    public Result register(@RequestBody User user){

        return mobileService.register(user);
    }

    @RequestMapping("/login")
    public Result login(@RequestBody User user){
        return mobileService.login(user);
    }

    @RequestMapping("/getLastPolicy")
    public Object getLastPolicy(){
        return mobileService.getLastPolicy();
    }
    @RequestMapping("/getPolicyList")
    public Result getPolicyList(){
        return mobileService.getPolicyList();
    }

    @RequestMapping("/upload")
    public Result upload(MultipartFile file){
        String fileName=file.getOriginalFilename();
        //重新设置新的名称
        String newFileName=System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));

        String path="D:\\upload5";

        File f=new File(path);
        if(!f.exists()){
            f.mkdirs();
        }


       try {
           file.transferTo(new File(path+File.separator+newFileName));
           String imageUrl="http://"+newFileName;
           System.err.println(imageUrl);
           return new Result(200,"文件上传成功",imageUrl);
       } catch (IOException e) {
           throw new RuntimeException();
       }

    }

    @RequestMapping("/getLoginUserInfo")
    public Result getLoginUserInfo(HttpServletRequest request){

        String token =request.getHeader("token");

        return mobileService.getLoginUserInfo(token);
    }

    @RequestMapping("/uploadHeadImage")
    public Result uploadHeadImage(MultipartFile file){
        return mobileService.uploadHeadImage(file);
    }
}
